<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
    <base href="<%=basePath%>">

    <meta charset="UTF-8">
    <title align='center'>图书管理总管理员后台</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.js"></script>
</head>
<body>
<script type="text/javascript">

    $(function () {
        //全选功能实现
        $("#checkAllCategory").click(function () {

            $("#tBody input[type = 'checkbox']").prop("checked", this.checked);
        });
        $("#tBody input[type = 'checkbox']").click(function () {
            if ($("#tBody input[type = 'checkbox']").length == $("#tBody input[type = 'checkbox']:checked").length) {
                $("#checkAllCategory").prop("checked", true);
            } else {
                $("#checkAllCategory").prop("checked", false);
            }
        });

        //该函数：收起用户输入的条件进行查询，如何将查询结果解析好展示在对应位置
        $("#librarySelectBtn").click(function () {
            var name = $("#name").val();
            var location = $("#location").val();
            $.ajax({
                url: 'queryLibrary?method=queryLibraryByCondition',
                data: {
                    libraryName: name,
                    libraryLocation: location
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    showBookForPage(data);
                }
            });
        });


            //点击添加按钮，展示div填写数据
            $("#addLibrary").click(function() {
                //每一次显示这个div，要把之前的值删掉
                $("#addLibraryDiv input[type='text']").val("");
                for (var i = 0; i < $("#addLibraryDiv").length; i++) {
                    $("#addLibraryDiv")[i].style.display = "block";
                }
                for (var j = 0; j < $("#libraryBtnDiv").length; j++) {
                    $("#libraryBtnDiv")[j].style.display = "block";
                }
            });

            //填写数据，点击确定按钮发送请求，请求成功隐没div
            $("#addBtn").click(function () {
                var libraryName = $.trim($("#libraryName").val());
                var libraryLocation = $.trim($("#libraryLocation").val());
                var userAccount = $.trim($("#userAccount").val());
                var password = $.trim($("#password").val());
                var curatorName = $.trim($("#curatorName").val());
                var gender = $.trim($("#gender").val());
                var phone = $.trim($("#phone").val());
                var email = $.trim($("#email").val());

                if (libraryName === "" || libraryName === null || libraryName === undefined) {
                    alert("图书馆名称不能为空");
                    return;
                }
                if (libraryLocation === "" || libraryLocation === null || libraryLocation === undefined) {
                    alert("图书馆位置不能为空");
                    return;
                }

                $.ajax({
                    url: 'addLibrary?method=addLibraryByCondition',
                    data: {
                        libraryName: libraryName,
                        libraryLocation: libraryLocation,
                        userAccount:userAccount,
                        password:password,
                        curatorName:curatorName,
                        gender:gender,
                        phone:phone,
                        email:email
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        var library = data.retData;
                        if (data.code == 1) {
                            $("#libraryTable").append('<tbody id="tBody"><tr><td><input type="checkbox" value=' + library.id + '/></td><td>'+library.libraryName+'</td><td>'+library.libraryLocation+'</td><td>'+library.libraryCurator+'</td></tr></tbody>');
                            //如果添加成功，div消失
                            for (var i = 0; i < $("#addLibraryDiv").length; i++) {
                                $("#addLibraryDiv")[i].style.display = "none";
                            }
                            for (var j = 0; j < $("#libraryBtnDiv").length; j++) {
                                $("#libraryBtnDiv")[j].style.display = "none";
                            }
                        }
                    }
                });
            });


            //添加图书馆div的取消按钮
            $("#cancelUpdateBtn").click(function () {
                for (var i = 0; i < $("#addLibraryDiv").length; i++) {
                    $("#addLibraryDiv")[i].style.display = "none";
                }
                for (var j = 0; j < $("#libraryBtnDiv").length; j++) {
                    $("#libraryBtnDiv")[j].style.display = "none";
                }
            });



            //为修改按钮绑定单击事件，点击之后查询数据显示对应的div
            $("#updateLibrary").click(function () {
                //获取所有选中书籍的id 修改一次只能改一个
                var checkedId = $("#tBody input[type = 'checkbox']:checked");
                if (checkedId.length != 1) {
                    alert("请选择一个要修改的图书馆");
                    return;
                }
                var id = checkedId.val();
                //发送请求，查询书籍，填在表单上，然后显示表单

                $.ajax({
                    url: 'selectLibrary?method=selectLibraryConditionById',
                    data: {
                        id:id
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        alert(data.message);
                        if (data.code == 1) {
                            //将数据填充进div,需要三个对象的数据，user，curator，library，填进
                            //list集合，俩年的对象依次是library，curator，user，先转json字符串
                            var json = JSON.stringify(data.retData);
                            //json字符串转化为json数组
                            var jsonArray = JSON.parse(json);
                            var library = jsonArray[0];
                            var curator = jsonArray[1];
                            var user = jsonArray[2];
                            //图书馆的id
                            $("#libraryTextId").val(library.id);
                            $("#updateLibraryName").val(library.libraryName);
                            $("#updateLibraryLocation").val(library.libraryLocation);
                            //用户的id
                            $("#userTextId").val(user.id);
                            $("#updateUserAccount").val(user.username);
                            $("#updatePassword").val(user.password);
                            //馆长的id
                            $("#curatorTextId").val(curator.id);
                            $("#updateCuratorName").val(curator.curatorName);
                            $("#updateGender").val(curator.gender);
                            $("#updatePhone").val(curator.phone);
                            $("#updateEmail").val(curator.email);
                            //更新图书馆的div显现
                            for (var i = 0; i < $("#updateLibraryDiv").length; i++) {
                                $("#updateLibraryDiv")[i].style.display = "block";
                            }
                            for (var j = 0; j < $("#updateLibraryBtnDiv").length; j++) {
                                $("#updateLibraryBtnDiv")[j].style.display = "block";
                            }
                        } else {
                            alert("操作失败");
                        }
                    }
                });
            });

            //为更新书籍的确定按钮绑定单击事件，点击发送请求
            $("#updateBtn").click(function () {
                var libraryId = $("#libraryTextId").val();
                var libraryName = $("#updateLibraryName").val();
                var libraryLocation = $("#updateLibraryLocation").val();
                //用户的id
                var userId = $("#userTextId").val();
                var username = $("#updateUserAccount").val();
                var password = $("#updatePassword").val();
                //馆长的id
                var curatorId = $("#curatorTextId").val();
                var curatorName = $("#updateCuratorName").val();
                var gender = $("#updateGender").val();
                var phone = $("#updatePhone").val();
                var email = $("#updateEmail").val();
                //非空判断的
                if (libraryName != null && libraryLocation != null) {
                    $.ajax({
                        url: 'updateLibrary?method=updateLibraryByCondition',
                        data: {
                            libraryId: libraryId,
                            libraryName: libraryName,
                            libraryLocation: libraryLocation,
                            userId:userId,
                            username:username,
                            password:password,
                            curatorId:curatorId,
                            curatorName:curatorName,
                            gender:gender,
                            phone:phone,
                            email:email
                        },
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            //展示查询结果其实就是再点一次查询按钮
                            $("#librarySelectBtn").click();
                            if (data.code == 1) {
                                //更新成功 给更新书籍的确定按钮绑定隐藏
                                for (var i = 0; i < $("#updateLibraryDiv").length; i++) {
                                    $("#updateLibraryDiv")[i].style.display = "none";
                                }
                                for (var j = 0; j < $("#updateLibraryBtnDiv").length; j++) {
                                    $("#updateLibraryBtnDiv")[j].style.display = "none";
                                }
                            }
                        }
                    });
                }
            });

            //添加图书馆div的取消按钮
            $("#cancelBtn").click(function () {
                for (var i = 0; i < $("#addLibraryDiv").length; i++) {
                    $("#addLibraryDiv")[i].style.display = "none";
                }
                for (var j = 0; j < $("#btnDiv").length; j++) {
                    $("#btnDiv")[j].style.display = "none";
                }
            });


            //更新图书馆div的取消按钮
            $("#cancelUpdateBtn").click(function () {
                for (var i = 0; i < $("#updateLibraryDiv").length; i++) {
                    $("#updateLibraryDiv")[i].style.display = "none";
                }
                for (var j = 0; j < $("#updateLibraryBtnDiv").length; j++) {
                    $("#updateLibraryBtnDiv")[j].style.display = "none";
                }
            });



            //先把简单的删除写了
            $("#deleteLibrary").click(function () {
                //获取选中的id
                var checkedIds = $("#tBody input[type='checkbox']:checked");
                var ids = "";
                $.each(checkedIds, function () {
                    ids += this.value + ',';
                });
                ids = ids.substr(0, ids.length - 1);
                //发送请求
                $.ajax({
                    url: 'deleteLibrary?method=deleteLibraryConditionByIds',
                    data: {
                        ids: ids
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        alert(data.message);
                        if (data.code == 1) {
                            $("#librarySelectBtn").click();
                        }
                    }

                });
            });



            //刷新列表的函数
            function showBookForPage(data) {
                if (data.code == 1) {
                    //每查询一次，先把上次查询写到表单的结果清理掉
                    $("#libraryTable tbody").html("");
                    //转json字符串
                    var json = JSON.stringify(data.retData);
                    //jspn字符串转化为json数组
                    var jsonArray = JSON.parse(json);
                    for (var i = 0; i < jsonArray.length; i++) {
                        $("#libraryTable").append('<tbody id="tBody"><tr><td><input type="checkbox" value=' + jsonArray[i].id + '/></td><td>' + jsonArray[i].libraryName + '</td><td>' + jsonArray[i].libraryLocation + '</td><td>'+jsonArray[i].libraryCurator+'</td></tr></tbody>');
                    }
                }
            }
    });
</script>
<br>
<h1 align="center">图书馆里总管理员后台</h1>
<hr>
<b>图书馆查询</b><br><br>
根据条件筛选，支持模糊查询<br><br>
名称:<input type="text" id="name" width="15px"><br>
位置:<input type="text" id="location" width="15px">
 <input type="button" value="查询" id="librarySelectBtn"/><br><br>
<table id="libraryTable" border="1px">
    <thead>
    <tr>
        <th><input type="checkbox" id="checkAllCategory"/>选择</th>
        <th>图书馆名称</th>
        <th>位置</th>
        <th>馆长</th>
    </tr>
    </thead>
</table><br>
<input type="button" id="addLibrary" value="添加"/>&nbsp;
<input type="button" id="updateLibrary" value="修改"/>&nbsp;
<input type="button" id="deleteLibrary" value="删除"/>&nbsp;
<br>
<div id="addLibraryDiv" style="display: none">
    <%--
      创建图书馆的时候，创建它的馆长
      staff_name
      gender
      phone
      email
      user_id
     permissions
     --%>
    添加图书馆<br>
    名称:<input type="text" id="libraryName"/><br>
    位置:<input type="text" id="libraryLocation"/><br>
    馆长相关信息<br>
    账号:<input type="text" id="userAccount">
    密码:<input type="text" id="password">
    姓名:<input type="text" id="curatorName"><br>
    性别:<input type="text" id="gender">
    电话:<input type="text" id="phone">
    邮箱:<input type="text" id="email">

</div>
<div id="libraryBtnDiv" style="display: none">
    <input type="button" value="确定" id="addBtn" />&nbsp;&nbsp;
    <input type="button" value="取消" id="cancelBtn" />
</div>

<div id="updateLibraryDiv"  style="display: none" >
        更新图书馆<br>
       <input  type="text" id="libraryTextId" style="display: none"/>
    名称:<input type="text" id="updateLibraryName"/><br>
    位置:<input type="text" id="updateLibraryLocation"/><br>
    馆长相关信息<br>
    <input type="text" id="userTextId" style="display: none">
    账号:<input type="text" id="updateUserAccount">
    密码:<input type="text" id="updatePassword">
    <input type="text" id="curatorTextId" style="display: none">
    姓名:<input type="text" id="updateCuratorName"><br>
    性别:<input type="text" id="updateGender">
    电话:<input type="text" id="updatePhone">
    邮箱:<input type="text" id="updateEmail">

</div>
<div id="updateLibraryBtnDiv" style="display: none">
        <input type="button" value="确定" id="updateBtn" />&nbsp;&nbsp;
        <input type="button" value="取消" id="cancelUpdateBtn" />
</div>
<br>
<a href="/exit">[安全退出]</a>
</body>
</html>
